//
//  UIStandardRadioSelectButton.m
//  vhStressTest1
//
//  Created by jia yu on 2021/8/31.
//

#import "UIStandardRadioSelectButton.h"
#import <vhECGTools/vhECGTools.h>
#import <ZMFoundation/ZMFoundation.h>

@interface UIStandardRadioSelectButton(){
    float screenRatio;
    UIImage *selectedImage;
    UIImage *unselectImage;
}
@end

@implementation UIStandardRadioSelectButton

-(void)setEnabled:(BOOL)_enabled{
    [super setEnabled:_enabled];
    [self refreshButton];
    return;
}

-(id)initWithFrame:(CGRect)frame{
    return [[UIStandardRadioSelectButton alloc] initWithFrame:frame scale:1.f];
}

-(id)initWithFrame:(CGRect)frame scale:(float)scale{
    self = [super initWithFrame:frame];
    if (self){
        screenRatio = scale;
        NSBundle *frameBundle = [NSBundle ex_bundleWithName:@"vhECGTools"];
        NSBundle *myBundle = [[NSBundle alloc] initWithPath:[frameBundle pathForResource:@"standard" ofType:@"bundle"]];
        selectedImage = [[UIImage alloc] initWithContentsOfFile:[myBundle pathForResource:@"radiobutton_select" ofType:@"png"]];
        UIImage *targetImage = [vhECGTools scaleImage:selectedImage to:CGSizeMake(90.f * screenRatio, 90.f * screenRatio)];
        [self setImage:targetImage forState:UIControlStateHighlighted];
        [self setImage:targetImage forState:UIControlStateSelected];
        
        unselectImage = [[UIImage alloc] initWithContentsOfFile:[myBundle pathForResource:@"radiobutton_unselect" ofType:@"png"]];
        targetImage = [vhECGTools scaleImage:unselectImage to:CGSizeMake(90.f * screenRatio, 90.f * screenRatio)];
        [self setImage:targetImage forState:UIControlStateNormal];
        
        self.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
        self.titleLabel.font = [UIFont fontWithName:@"PingFangSC-Regular" size:34.f * screenRatio];
        self.titleLabel.textAlignment = NSTextAlignmentLeft;
        [self setTitleColor:[vhECGTools colorFromHEXString:@"294559"] forState:UIControlStateNormal];
        [self setTitleColor:[vhECGTools colorFromHEXString:@"294559" withAlpha:0.4] forState:UIControlStateDisabled];
        self.titleEdgeInsets = UIEdgeInsetsMake(0.f, -10.f * screenRatio, 0.f, +10.f * screenRatio);
        [self refreshButton];
    }
    return self;
}

-(void)refreshButton{
    UIImage *selected = [vhECGTools scaleImage:selectedImage to:CGSizeMake(90.f * screenRatio, 90.f * screenRatio)];
    UIImage *unselect = [vhECGTools scaleImage:unselectImage to:CGSizeMake(90.f * screenRatio, 90.f * screenRatio)];
    if (self.enabled){
        [self setImage:selected forState:UIControlStateHighlighted];
        [self setImage:selected forState:UIControlStateSelected];
        [self setImage:unselect forState:UIControlStateNormal];
        [self setTitleColor:[vhECGTools colorFromHEXString:@"294559"] forState:UIControlStateNormal];
    }else{
        if (self.selected){
            [self setImage:selected forState:UIControlStateNormal];
        }else{
            [self setImage:unselect forState:UIControlStateNormal];
        }
        [self setTitleColor:[vhECGTools colorFromHEXString:@"294559" withAlpha:0.4] forState:UIControlStateNormal];
    }
}

@end
